//
//  LPFEmoticonKeyboardView.swift
//  WeiBo
//
//  Created by mac on 16/4/24.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class LPFEmoticonKeyboardView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RandomColor()
        
        addSubview(emoticonToolBar)
        addSubview(emoticonPageView)
        addSubview(pageControl)
        
        emoticonToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonPageView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(emoticonToolBar.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(emoticonPageView)
        }
        // MARK: -  emoticonToolBar 实现联动
        emoticonToolBar.emoticonToolBarClosure = {[weak self] (type: LPFEmoticonToolBarType) in
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            // 把 emoticonPageView滚动到指定的indexPath
            self?.emoticonPageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            // pageControl 的总数
            self?.pageControl.numberOfPages = LPFEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
            // 当前选中的
            self?.pageControl.currentPage = 0
        }
        
    }
    
    // MARK: -  底部toolbar
    private lazy var emoticonToolBar: LPFEmoticonToolBar = LPFEmoticonToolBar()
    
    // MARK: - 表情键盘
    private lazy var emoticonPageView: LPFEmoticonPageView = {
       let view = LPFEmoticonPageView()
        
        //取消弹簧效果
        view.bounces = false
        //分页
        view.pagingEnabled = true
        //设置代理
        view.delegate = self
        //滚动条隐藏
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        
        return view
    }()
    // MARK: - pageControl
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        // 总页数
        pageControl.numberOfPages = LPFEmoticonTools.sharedTools.allEmoticons[0].count
        // 当前选中的页数
        pageControl.currentPage = 0
        // kvc 给私有属性设置image
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        return pageControl
    }()
}

extension LPFEmoticonKeyboardView: UICollectionViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
        // 得到相对于我们scrollView的x 中心点
        let centerX = SCREENW / 2  + scrollView.contentOffset.x
        // 给数组排序
        let cells = emoticonPageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.x
        }
        // 只有屏幕显示两个cell的时候我们才会切换选中的按钮
        if cells.count == 2{
            var indexPath: NSIndexPath?
            
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            // 判断我们当前的cell 的frame 是否包含 一个点坐标
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)){
                indexPath = emoticonPageView.indexPathForCell(firstCell)!
            }else{
                indexPath = emoticonPageView.indexPathForCell(lastCell)!
            }
            // 设置选中按钮状态
            emoticonToolBar.setEmoticonCurrentButton(indexPath!.section)
            // pageControl 的总数
            pageControl.numberOfPages = LPFEmoticonTools.sharedTools.allEmoticons[indexPath!.section].count
            // 当前选中的
            pageControl.currentPage = indexPath!.item
        }
    }
}
